/**
 * 
 */
package com.rs.controller;

import java.awt.EventQueue;

import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;

import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.table.DefaultTableModel;

import com.github.pagehelper.PageInfo;
import com.rs.model.ZhGoOut;
import com.rs.model.ZhUser;
import com.rs.service.ZhGoOutService;
import com.rs.util.SpringContextUtils;

import javax.swing.SpringLayout;

import java.awt.event.ActionEvent;

import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;


/**
 * @author zjw
 *
 */

@SuppressWarnings("serial")
public class GoOutList extends JInternalFrame {
	
	

	private JPanel contentPane;
	
	private JScrollPane jp;
	
	private JTable jt;
	
	private Object[][] datas;
	

	int pageSum=1;
	
	ZhGoOutService gooutservice = SpringContextUtils.getBean(ZhGoOutService.class);
	private JButton btn_start;
	private JButton btn_next;
	private JButton btn_end;
	private JLabel lb_page;
	String[] titles = {"主键","申请人名称","开始时间","结束时间","时长（小时）","事由","图片地址","审核人名称","审核状态"};
	private JTextField txt_se_auname;
	private JTextField txt_se_name;

	ZhGoOut goout = new ZhGoOut();
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GoOutList frame = new GoOutList(null,null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @param desktopPane_1 
	 * @param user 
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public GoOutList(final JDesktopPane desktopPane_1, final ZhUser user) {
		setIconifiable(true);
		setMaximizable(true);
		setClosable(true);
		setResizable(false);
		
	
		setTitle("外出审核列表");
//		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1054, 670);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);		
		
		jt = new JTable(new Object[][]{{null,null,null,null,null,null,null,null,null},},titles);
	
		
		 jt.setEnabled(true);
		
		
		
		
		// Step1 添加行点击绑定事件 
		jt.addMouseListener(new MouseAdapter() { 
		@Override 
		public void mouseClicked(MouseEvent e) {                 
			rowClicked();             
			}
		/** *行点击事件 * 
		*<br>-----------------------------------------------------<br> *@author flotage 
		*@create 2013-4-17上午10:09:14 *@note */ 
		private void rowClicked() {
			// 获取被点击的行 
			int row = jt.getSelectedRow(); 
			// 获取点击行的内容 
			        DefaultTableModel dtm = (DefaultTableModel) jt.getModel();         
			        Object value =  dtm.getValueAt(row, 0);          
			     
			        GoOutUpdate att = new GoOutUpdate(desktopPane_1,user);
			        att.select(value);
			        doDefaultCloseAction();
			        att.setVisible(true);
			
		}         
		});  
		SpringLayout sl_contentPane = new SpringLayout();
		contentPane.setLayout(sl_contentPane);
	
		
		
		jp = new JScrollPane(jt);
		sl_contentPane.putConstraint(SpringLayout.NORTH, jp, 150, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, jp, 5, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, jp, 550, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, jp, 1033, SpringLayout.WEST, contentPane);
		jp.setSize(854, 640);
		contentPane.add(jp);
		
		btn_start = new JButton("首页");
		btn_start.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pageSum = 1;
				goout.setPageNum(pageSum);
				List<ZhGoOut> list = getGoOutlist(goout).getList();
				SetTheValue(list);
			}
		});
		contentPane.add(btn_start);
		
		JButton btn_up = new JButton("上一页");
		btn_up.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				goout.setPageNum(pageSum);
				upPage(getGoOutlist(goout).getPrePage());
			}
		});
		sl_contentPane.putConstraint(SpringLayout.NORTH, btn_start, 0, SpringLayout.NORTH, btn_up);
		sl_contentPane.putConstraint(SpringLayout.EAST, btn_start, -14, SpringLayout.WEST, btn_up);
		sl_contentPane.putConstraint(SpringLayout.NORTH, btn_up, 24, SpringLayout.SOUTH, jp);
		contentPane.add(btn_up);
		
		btn_next = new JButton("下一页");
		btn_next.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				goout.setPageNum(pageSum);
				nextPage(getGoOutlist(goout).getNextPage());
			}
		});
		sl_contentPane.putConstraint(SpringLayout.NORTH, btn_next, 24, SpringLayout.SOUTH, jp);
		sl_contentPane.putConstraint(SpringLayout.EAST, btn_up, -116, SpringLayout.WEST, btn_next);
		contentPane.add(btn_next);
		
		btn_end = new JButton("尾页");
		btn_end.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pageSum = getGoOutlist(goout).getPages();
				goout.setPageNum(pageSum);
				List<ZhGoOut> list = getGoOutlist(goout).getList();
				SetTheValue(list);
				
				lb_page.setText("当前是第"+getGoOutlist(goout).getPages()+"页");
			}
		});
		sl_contentPane.putConstraint(SpringLayout.NORTH, btn_end, 24, SpringLayout.SOUTH, jp);
		sl_contentPane.putConstraint(SpringLayout.EAST, btn_next, -23, SpringLayout.WEST, btn_end);
		sl_contentPane.putConstraint(SpringLayout.EAST, btn_end, -41, SpringLayout.EAST, contentPane);
		contentPane.add(btn_end);
		
		lb_page = new JLabel();
		sl_contentPane.putConstraint(SpringLayout.NORTH, lb_page, 2, SpringLayout.NORTH, btn_start);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, lb_page, -3, SpringLayout.SOUTH, btn_start);
		sl_contentPane.putConstraint(SpringLayout.EAST, lb_page, -23, SpringLayout.WEST, btn_next);
		contentPane.add(lb_page);
		
		JLabel lb_se_name = new JLabel("申请人名称：");
		sl_contentPane.putConstraint(SpringLayout.NORTH, lb_se_name, 53, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, lb_se_name, -861, SpringLayout.EAST, contentPane);
		contentPane.add(lb_se_name);
		
		txt_se_name = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, txt_se_name, -3, SpringLayout.NORTH, lb_se_name);
		sl_contentPane.putConstraint(SpringLayout.WEST, txt_se_name, 18, SpringLayout.EAST, lb_se_name);
		contentPane.add(txt_se_name);
		txt_se_name.setColumns(10);

		
		JLabel lb_se_audit = new JLabel("审核状态：");
		sl_contentPane.putConstraint(SpringLayout.EAST, txt_se_name, -55, SpringLayout.WEST, lb_se_audit);
		sl_contentPane.putConstraint(SpringLayout.NORTH, lb_se_audit, 0, SpringLayout.NORTH, lb_se_name);
		sl_contentPane.putConstraint(SpringLayout.EAST, lb_se_audit, -616, SpringLayout.EAST, contentPane);
		contentPane.add(lb_se_audit);
		
		final JComboBox cbo_se_audit = new JComboBox();
		cbo_se_audit.setModel(new DefaultComboBoxModel(new String[] {"","待审核", "审核成功", "审核失败"}));
		sl_contentPane.putConstraint(SpringLayout.NORTH, cbo_se_audit, -3, SpringLayout.NORTH, lb_se_name);
		sl_contentPane.putConstraint(SpringLayout.WEST, cbo_se_audit, 19, SpringLayout.EAST, lb_se_audit);
		sl_contentPane.putConstraint(SpringLayout.EAST, cbo_se_audit, 131, SpringLayout.EAST, lb_se_audit);
		contentPane.add(cbo_se_audit);
		
		JLabel lb_se_auname = new JLabel("审核人名称：");
		sl_contentPane.putConstraint(SpringLayout.NORTH, lb_se_auname, 0, SpringLayout.NORTH, lb_se_name);
		sl_contentPane.putConstraint(SpringLayout.WEST, lb_se_auname, 0, SpringLayout.WEST, btn_start);
		contentPane.add(lb_se_auname);
		
		txt_se_auname = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, txt_se_auname, -3, SpringLayout.NORTH, lb_se_name);
		sl_contentPane.putConstraint(SpringLayout.WEST, txt_se_auname, 26, SpringLayout.EAST, lb_se_auname);
		sl_contentPane.putConstraint(SpringLayout.EAST, txt_se_auname, -248, SpringLayout.EAST, contentPane);
		contentPane.add(txt_se_auname);
		txt_se_auname.setColumns(10);
		
		JButton btn_select = new JButton("查询");
		sl_contentPane.putConstraint(SpringLayout.NORTH, btn_select, -4, SpringLayout.NORTH, lb_se_name);
		sl_contentPane.putConstraint(SpringLayout.WEST, btn_select, 47, SpringLayout.EAST, txt_se_auname);
		sl_contentPane.putConstraint(SpringLayout.EAST, btn_select, -96, SpringLayout.EAST, contentPane);
		btn_select.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pageSum = 1;
				goout.setPageNum(pageSum);
				if(txt_se_name.getText() != null){
					goout.setGoUname(txt_se_name.getText());
				}
				
				if(cbo_se_audit.getSelectedItem() != ""){
					goout.setGoAudit(cbo_se_audit.getSelectedIndex()-1);
				}
				if(txt_se_auname.getText() != null){
					goout.setGoAuname(txt_se_auname.getText());
				}
				
				List<ZhGoOut> list = gooutservice.selectGoOutAudit(goout).getList();
				goout.setGoAudit(null);
				SetTheValue(list);
				
				
			}
		});
		contentPane.add(btn_select);
		
		
		goout.setPageNum(pageSum);
		List<ZhGoOut> list = getGoOutlist(goout).getList();
				
				
		SetTheValue(list);
	
	}
	
	public PageInfo<ZhGoOut> getGoOutlist(ZhGoOut pagenum){

		PageInfo<ZhGoOut> pageinfo = gooutservice.selectGoOutAudit(pagenum);
		
		return pageinfo;
		
	}
	
	public void nextPage(int poid){
		  
		pageSum++; 
		  goout.setPageNum(pageSum);
		  List<ZhGoOut> attendance = getGoOutlist(goout).getList();
		  if(attendance.size() == 0){
		   JOptionPane.showMessageDialog(this, "当前已是最后一页！");
		   pageSum--;
		   goout.setPageNum(pageSum);
		   attendance = getGoOutlist(goout).getList();
		   SetTheValue(attendance);
		   
		   return;
		  }
		  SetTheValue(attendance);
		 }
		 

		 
		 public void upPage(int poid){
		  if(pageSum<=1){
		   JOptionPane.showMessageDialog(this, "当前是第一页！");
		   goout.setPageNum(pageSum);
		   List<ZhGoOut> student = getGoOutlist(goout).getList();
		   SetTheValue(student);
		   return;
		  }
		  pageSum--;
		  goout.setPageNum(pageSum);
		  List<ZhGoOut> student = getGoOutlist(goout).getList();
		  SetTheValue(student);
		  
		 }
		 public void SetTheValue(List<ZhGoOut> list){
			
				lb_page.setText("当前是第"+pageSum+"页");
				
			datas = new Object[list.size()][9];
				if(list.size()>0){
					for (int i = 0; i < list.size(); i++)
					{
						for (int j = 0; j < 9; j++)
						{
							switch (j)
							{
							case 0:
								datas[i][j] = list.get(i).getGoId();
								break;
							case 1:
								datas[i][j] = list.get(i).getGoUname();
								break;
							case 2:
								datas[i][j] = list.get(i).getGoKtimes();
								break;
							case 3:
								datas[i][j] = list.get(i).getGoJtimes();
								break;
							case 4:
								datas[i][j] = list.get(i).getGoLength();
								break;
							case 5:
								datas[i][j] = list.get(i).getGoReason();
								break;
							case 6:
								datas[i][j] = list.get(i).getGoImg();
								break;
							case 7:
								
								datas[i][j] = list.get(i).getGoAuname();
								break;
							case 8:
								if(list.get(i).getGoAudit() == 0){
									datas[i][j] = "待审核";
								}
								if(list.get(i).getGoAudit() == 1){
									datas[i][j] = "审核成功";
								}
								if(list.get(i).getGoAudit() == 2){
									datas[i][j] = "审核失败";
								}
						
								break;
							
							
							}
						}
					}
				}
				
			DefaultTableModel dftablemodel = new DefaultTableModel(datas,titles);
			jt.setModel(dftablemodel);
			jt.getColumnModel().getColumn(0).setMinWidth(0);
			jt.getColumnModel().getColumn(0).setMaxWidth(0);
			
			}
}
